<?php
namespace app\modules\models;

use yii\behaviors\BlameableBehavior;
use yii\db\ActiveRecord;
use Yii;
use yii\helpers\ArrayHelper;

class Menu extends ActiveRecord{


    public function behaviors()
    {
        return [
            [
                'class' => BlameableBehavior::className(),
                'createdByAttribute' => 'cuid',
                'updatedByAttribute' => 'uuid',
                'value' => Yii::$app->admin->id,
            ],
        ];
    }

    public static function tableName()
    {
        return "{{%menu}}";
    }

    public function rules()
    {
        return [
            ['url','required','message' => '菜单连接不能为空'],
            ['label','required','message' => '菜单名称不能为空'],
            ['module','required','message' => '控制器模型不能为空'],
            ['icon','required','message' => '菜单logo不能为空'],
            ['sort','required','message' => '排序不能为空'],
            ['sort','number','message' => '排序必须是数字'],
            ['parentid','required','message' => '关联菜单不能为空'],
            ['createtime','safe'],//创建时间是安全的，否则添加数据时为空
        ];
    }



    public function attributeLabels()
    {
        return [
            'url'=>'菜单连接',
            'label'=>'菜单名称',
            'module'=>'控制器模型',
            'icon'=>'菜单logo',
            'sort'=>'排序',
            'parentid'=>'关联菜单',
        ];
    }


    public function add($data)
    {
        //$this->cuid = Yii::$app->admin->id;//后台登录的id
        $this->createTime = time();
        if($this->load($data)&&$this->save()){//save方法有验证和添加的功能
            return true;
        }
        return false;
    }

    //获取数据
    public function getData(){
        $cates = self::find()->all();//查出来的全部对象
        $cates = ArrayHelper::toArray($cates);//转化为数据
        return $cates;
    }
    //获取无限极树
    public function getTree($cates,$pid=0){
        $tree = [];
        foreach ($cates as $cate){
            if($cate['parentid'] == $pid){
                $tree[]=$cate;
                $tree = array_merge($tree,$this->getTree($cates,$cate['cateid']));//回调自己，无限极分类
            }
        }
        return $tree;
    }
    //给无限极分类加前缀
    public function setPrefix($data,$p = '|--'){
        $tree = [];
        $num = 1;
        $prefix = [0=>1];
        while ($val = current($data)){ //输出当前元素的值
            $key = key($data);//输出键名
            if($key>0){
                if($data[$key-1]['parentid'] != $val['parentid']){
                    $num++;
                }
            }
            if(array_key_exists($val['parentid'],$prefix)){ //判断键名是否存在数组中
                $num = $prefix[$val['parentid']];
            }
            $val['label'] = str_repeat($p,$num).$val['label'];//字符串出现次数
            $prefix[$val['parentid']] = $num;//0->1
            $tree[]=$val;
            next($data);//输出当前元素的下一个元素
        }
        return $tree;
    }

    //获取菜单集合
    public function getOptions(){
        $data=$this->getData();
        $tree = $this->getTree($data);
        $tree = $this->setPrefix($tree);
        $options = ['0'=>'添加顶级菜单'];
        foreach ($tree as $cate){
            $options[$cate['cateid']] = $cate['label'];
        }
        return $options;
    }

    //获取树形结构的数据
    public function getTreeList()
    {
        $data = $this->getData();
        $tree = $this->getTree($data);
        return $tree = $this->setPrefix($tree);
    }

    public function getMenuList(){
        $data=$this->getData();
        $tree = $this->getMenuTree($data);
        return $tree;
    }

    public function getMenuTree($cates,$pid=0){
        $tree = [];
        foreach ($cates as $k=>$cate){
            if($cate['parentid'] == $pid){
                $cate['submenu']=$this->getTree($cates,$cate['cateid']);//回调自己，无限极分类
                $tree[]=$cate;
            }
        }
        return $tree;
    }


}
